<template>
  <div>
    <el-form :inline="true" size="small">
      <el-form-item>
        <el-input v-model="searchForm.realname" placeholder="公告标题" clearable>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getNoticList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="addNotic()">新增公告</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="noticData"
      tooltip-effect="dark"
      style="width: 100%"
      border
      stripe
    >
       <el-table-column width="55" label="序号">
        <template slot-scope="scope">
          <span>{{ (page - 1) * limit + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" prop="notice_title" label="公告标题"> </el-table-column>
      <el-table-column prop="notice_type" label="公告类型"> 
        <template slot-scope="scope">
          <span v-if="scope.row.notice_type == 1">系统公告</span>
          <span v-if="scope.row.notice_type == 2">普通公告</span>
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间"> </el-table-column>
      <el-table-column prop="icon" width="260px" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="infoNotic(scope.row)">查看</el-button>
          <el-button v-if="isAuth('company/list/edit')" type="text" @click="editNotic(scope.row)">编辑</el-button>
          <el-button v-if="isAuth('company/list/del')" type="text" @click="delNotic(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="page"
      :page-size="limit"
      :total="total"
    >
    </el-pagination>

    <!--公告对话框-->
    <el-dialog
      :title="noticType === 'add'
          ? `新增公告`
          : noticType === 'edit'
          ? '编辑公告'
          : noticType === 'delete'
          ? '删除公告'
          : ''"
      :visible.sync="noticFlag"
      width="800px"
      :before-close="noticClose"
    >
      <el-form :model="noticForm" :rules="noticFormRules" ref="noticForm" size="small" v-show="noticType === 'add' || noticType === 'edit'">
        <el-form-item label="公告标题" prop="notice_title" label-width="100px">
          <el-input v-model="noticForm.notice_title" placeholder="请输入公告标题"></el-input>
        </el-form-item>

        <el-form-item label="公告类型" prop="notice_type" label-width="100px">
          <el-radio-group v-model="noticForm.notice_type">
            <el-radio :label="1">系统公告</el-radio>
            <el-radio :label="2">普通公告</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="附件" prop="notice_attachment" label-width="100px">
          <el-upload action="admin/index/uploadFile">
            <el-button size="small" type="primary" >点击上传</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="内容" prop="notice_content" label-width="100px">
          <Editor v-if="noticFlag" ref="editor" :editorContent="noticForm" @change="change"></Editor>
        </el-form-item>

      </el-form>
      <div v-show="noticType === 'delete'">
        确认删除公告【{{noticForm.notice_title}}】？
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="resetForm('noticForm')">取 消</el-button>
        <el-button size="small" type="primary" @click="submitForm('noticForm')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 查看对话框 -->
    <el-dialog title="公告信息" :visible.syn="infoFlag" @close="infoFlag=false">
      <div style="max-height:60vh;overflow:auto;">
        <el-descriptions :column="1" border>
          <el-descriptions-item labelStyle="width:20%" label="公告标题">{{noticForm.notice_title}}</el-descriptions-item>
          <el-descriptions-item label="公告类型">
            <span v-if="noticForm.notice_type == 1">系统公告</span>
            <span v-if="noticForm.notice_type == 2">普通公告</span>
          </el-descriptions-item>
          <el-descriptions-item label="公告内容">
            <div v-html="noticForm.notice_content"></div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Editor from "../components/editor.vue"
export default {
    data() {
        return {
            searchForm: {},
            total: 0,
            page: 1,
            limit: 10,
            noticData: [],
            noticFlag: false,
            noticType:'',

            noticForm: {},
            noticFormRules: {
                notice_title: [
                    { required: true, message: "请输入公告标题", trigger: "blur" },
                ],
            },
            infoFlag:false,
        };
    },
    components : {
        Editor
    },
    created() {
        this.getNoticList();
    },
    methods: {
        handleSizeChange(val) {
            this.limit = val;
            this.getNoticList();
        },
        handleCurrentChange(val) {
            this.page = val;
            this.getNoticList();
        },
        //重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.noticFlag = false;
            this.noticFlag2 = false;
            this.noticForm = {};
        },
        //关闭表单
        noticClose() {
            this.resetForm("noticForm");
        },
        //获取公告列表
        getNoticList() {
            let data = {
                page: this.page,
                limit: this.limit,
                search: this.searchForm,
            };
            this.$axios.post(this.API.GetNoticList, data).then((res) => {
                this.noticData = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        // 获取富文本内容
        change(data) {
          this.noticForm.notice_content = data
        },
        //新增，编辑公告
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.noticType == 'add') {
                      this.$axios.post(this.API.AddNotic,this.noticForm).then(() => {
                        this.$message({type: "success",message: "添加成功",});
                        this.getNoticList()
                      })
                    } else if (this.noticType == 'edit') {
                      this.$axios.post(this.API.EditNotic,this.noticForm).then(() => {
                        this.$message({type: "success",message: "编辑成功",});
                        this.getNoticList()
                      })
                    } else if (this.noticType == 'delete') {
                      this.$axios.post(this.API.DelNotic,{id:this.noticForm.notice_id}).then(() => {
                        this.$message({type: "success",message: "删除成功",});
                        this.getNoticList()
                      })
                    }
                    this.noticForm = {};
                    this.noticFlag = false;
                }
            });
        },
        //新增公告
        addNotic() {
          this.noticForm = {}
          this.$set(this.noticForm,'notice_type',1)
          this.noticType = 'add';
          this.noticFlag = true;
        },
        //编辑公告
        editNotic(e) {
          this.noticForm = JSON.parse(JSON.stringify(e));
          this.noticType = 'edit';
          this.noticFlag = true;
        },
        //删除公告
        delNotic(e) {
          this.noticForm = JSON.parse(JSON.stringify(e));
          this.noticType = 'delete';
          this.noticFlag = true;
        },
        // 查看公告
        infoNotic(e) {
          this.noticForm = e
          this.infoFlag = true
        }
    },
    components: { Editor }
};
</script>

<style scoped lang="scss">
.el-pagination {
  float: right;
  margin-top: 22px;
}
.el-dialog {
  .el-input {
    width: 300px;
  }
}

</style>